const util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    kongImage: false,
    isFenXiao: true,
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    isLieBiao: false,
    desc: 'desc',
    order_list: '',
    loading: false,
    pageNum: 1,
    showNum: 6,
    list: [],
    up: true,
    currentTab: 0,
    array: [1, 2, 3, 4, 5, 1, 2, 3, 4],
    navArr: [{
      name: '综合',
      type: "",
      desc: 'desc'
    }, {
      name: '销量',
      type: "goods_salecount",
      desc: 'desc'
    }, {
      name: '新品',
      type: "creat_at",
      desc: 'desc'
    }, {
      name: '价格',
      type: "low_goods_price",
      desc: 'desc'
    }],
  },

  click(e) {
    this.data.pageNum = 1
    this.setData({
      list: [],
      desc: 'desc',
      kongImage: false
    })
    var dataset = e.currentTarget.dataset,
      currentTab = dataset.index,
      item = dataset.item;
    this.setData({
      currentTab,
      order_list: item.type,
      loading: false,
      ['navArr[' + dataset.index + '].desc']: item.desc == 'desc' ? 'asc' : 'desc',
    })
    if (dataset.index == 3){
      this.setData({
        desc: item.desc == 'desc' ? 'asc' : 'desc'
      })
    }
    this.getShopList()
  },
  changeFenLei() {
    this.setData({
      isLieBiao: !this.data.isLieBiao
    })
  },
  jumpDetails(e) {

    var that = this
    // util.API('spwx/v1.Shop/goodsinfo', {
    //   goods_id: e.currentTarget.dataset.item.goods_id
    // }).then(res => {
    //   var {
    //     data,
    //     code
    //   } = res
    // if (code === 200) {
    wx.navigateTo({
      url: '/pages/shopDetails/shopDetails?goods_id=' + e.currentTarget.dataset.item.goods_id
    })
    //   } else {
    //     wx.showToast({
    //       title: '该商品已经下架',
    //       icon: 'none'
    //     })
    //   }
    // })


  },
  jumpSearch() {
    wx.navigateTo({
      url: '../Search/Search'
    })
  },
  jumpMenu() {
    wx.navigateTo({
      url: '/pages/Commodity/Commodity',
    })
  },
  setNavigationBarTitle(name) { // 设置title名称
    wx.setNavigationBarTitle({
      title: name
    })
  },
  onLoad: function(options) {
    console.log(options)
    this.setData({
      coupon: options.coupon ? options.coupon : '',
      jump_id: options.jump_id ? options.jump_id:options.goodstypeid ? options.goodstypeid : ''
    })
    this.data.options = options
    this.setNavigationBarTitle(options.title + '')
   
  },
onShow(){
  this.data.pageNum=1;
  this.data.list=[];
  this.getShopList()
},
  onReachBottom() {
    this.data.pageNum++
      this.getShopList()
  },
   async getShopList() {
      let {
        pageNum,
        options,
        page,
        order_list,
        jump_id,
        desc
      } = this.data
     await util.API('spwx/v1.Shop/shop_list', {
        goods_type: jump_id,
        page: pageNum,
        order_list,
        desc,
        goods_name: options.val ? options.val : "",
        coupon: this.data.coupon ? this.data.coupon : ''
      }).then(res => {
        let {
          code,
          data
        } = res
        if (code == 200) {  
          this.data.list = this.data.list.concat(data.list);
          this.setData({
            list: this.data.list,
          })
        } else {
          this.setData({
            loading: true,
          })
        }
        if (this.data.list.length == 0) {
          this.setData({
            kongImage: true
          })
        }
      })
    }
 
}))